using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using MVP.Demo.Core;
using MVP.Demo.Presentation;
using MVP.Demo.View;

namespace MVP.Demo.Web
{
    public partial class PlayerList : Page, IPlayerListView
    {
        private PlayerListPresenter presenter;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            WireUpEvents();
            presenter = new PlayerListPresenter(this);
        }

        private void WireUpEvents()
        {
            saveButton.Click += new EventHandler(saveButton_Click);
            playerRepeater.ItemCommand += new RepeaterCommandEventHandler(playerRepeater_ItemCommand);
        }

        void playerRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                presenter.DeletePlayer(e.CommandArgument.ToString());
            }
        }

        void saveButton_Click(object sender, EventArgs e)
        {
            if (SavePlayer != null)
            {
                SavePlayer(sender, e);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            presenter.InitializeView();
        }

        public int TeamID
        {
            get { return 1; }
        }

        public List<PlayerDTO> ListOfPlayers
        {
            set
            {
                playerRepeater.DataSource = value;
                playerRepeater.DataBind();
            }
        }

        public bool IsPagePostBack
        {
            get { return IsPostBack; }
        }

        public event EventHandler SavePlayer;

        public string FirstName
        {
            get { return firstNameTextBox.Text; }
        }

        public string LastName
        {
            get { return lastNameTextBox.Text; }
        }
    }
}
